<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Search extends Model
{
    protected $table = 'search';
    protected $primaryKey = 'id';
    protected $guarded = [];
    const VISITED_EXPIRE = 86400;


    public static function record($key,$ip)
    {
        $visit = self::where('keyword',$key)
            ->where('client_ip',$ip)
            ->where('updated_at', '>', \DB::raw('NOW()-'.self::VISITED_EXPIRE.''))
            ->first();
        if(!$visit){
            self::create(['keyword'=>$key,'client_ip'=>$ip]);
        }
    }

    public function getCountAttribute()
    {
        return $this->where('keyword',$this->keyword)->count();
    }
}
